//
//  DJCollectionViewCell.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/7.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "DJCollectionViewCell.h"
#import "DJTableViewCell.h"
#import "DJBannerTableViewCell.h"
#import "GXNetTool.h"
#import "CFMacro.h"
#import "UIView+Frame.h"
#import "GXNetTool.h"

// model content
#import "DJRootModel.h"
#import "Dj_Info.h"
#import "Banners.h"
// model location
#import "DJLocationRoot.h"
#import "DJ_location.h"
#import "DJLocation_info.h"
#import "DJDetailViewController.h"
#import "DropViewCollectionViewCell.h"

#import "DJDetailBannerViewController.h"

#import "ZLDropDownMenu.h"

#define kDJTableViewCell @"DJTableViewCell"
#define kDJBannerTableViewCell @"DJBannerTableViewCell"
#define kDropViewCollectionViewCell @"DropViewCollectionViewCell"

// , DOPDropDownMenuDataSource,DOPDropDownMenuDelegate
@interface DJCollectionViewCell () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, ZLDropDownMenuDataSource, ZLDropDownMenuDelegate>

// tableView
@property (weak, nonatomic) IBOutlet UITableView *tableView;
// 搜索视图
@property (weak, nonatomic) IBOutlet UIView *searchView;
// 地区视图
@property (nonatomic, strong) UICollectionView *collectionView;
// 排序视图
@property (weak, nonatomic) IBOutlet UIView *orderView;
// 下拉菜单
//@property (weak, nonatomic) IBOutlet UIView *menuView;
// 排序按钮
@property (weak, nonatomic) IBOutlet UIButton *upToDateButton;
// 地区按钮
@property (weak, nonatomic) IBOutlet UIButton *locationButton;
// 记录选择了那个地区
@property (nonatomic, strong) NSIndexPath *index_path;
// 记录地区名字
@property (nonatomic, copy) NSString *locationName;
@property (nonatomic, strong) UIViewController *vc;
// 下拉菜单用到的数组
@property (nonatomic, strong) NSMutableArray *subTitles;
@property (nonatomic, strong) NSMutableArray *mainTitles;

@end

@implementation DJCollectionViewCell

#pragma mark - 按照最热排序
- (IBAction)hotButtonClicked:(id)sender {
    NSLog(@"按照最热排序");
}
#pragma mark - 按照最新排序
- (IBAction)newButtonClicked:(id)sender {
    NSLog(@"按照最新顺序排序");
}
#pragma mark - 点击排序按钮
- (IBAction)orderButtonClicked:(id)sender {
    UIButton *button = sender;
    button.selected = !button.selected;
    if (button.selected) {
        [self showOrderView];
    } else {
        [self closeOrderView];
    }
    _collectionView.transform = CGAffineTransformIdentity;
    _locationButton.selected = !_locationButton.selected;
}
#pragma mark - 点击地区按钮
- (IBAction)locationButtonClicked:(id)sender {
    _orderView.transform = CGAffineTransformIdentity;
    UIButton *button = sender;
    button.selected = !button.selected;
    if (button.selected) {
        [self showCollectionView];
    } else {
        [self closeCollectionView];
    }
}
#pragma mark - 显示地区窗口
- (void)showCollectionView {
    [UIView animateWithDuration:0.5 animations:^{
        _collectionView.transform = CGAffineTransformMakeTranslation(0, 230);
    }];
}
#pragma mark - 隐藏地区窗口
- (void)closeCollectionView {
    [UIView animateWithDuration:0.5 animations:^{
        _collectionView.transform = CGAffineTransformIdentity;
    }];
}
#pragma mark - 显示排序窗口
- (void)showOrderView {
    [UIView animateWithDuration:0.5 animations:^{
        _orderView.transform = CGAffineTransformMakeTranslation(0, 90);
    }];
}
#pragma mark - 隐藏排序窗口
- (void)closeOrderView {
    [UIView animateWithDuration:0.5 animations:^{
        _orderView.transform = CGAffineTransformIdentity;
    }];
}
#pragma mark - 弹出地区窗口
- (void)createDownPullMenu {
    // 添加下拉菜单
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -200, SCREEN_WIDTH, 30 * 5) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 6.0, 30);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 149, SCREEN_WIDTH, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    line.alpha = 0.5;
    [_collectionView addSubview:line];
    // 注意层级关系
//    [self.contentView bringSubviewToFront:_menuView];
//    [self.contentView insertSubview:_collectionView belowSubview:_menuView];
    kCollectionViewRegisterNib(@"DropViewCollectionViewCell", kDropViewCollectionViewCell);
}
#pragma mark - 下拉菜单代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _location.location_info.location.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DropViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDropViewCollectionViewCell forIndexPath:indexPath];
    cell.dj_location = _location.location_info.location[indexPath.row];
    return cell;
}
#pragma mark - 选中item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 收回地区弹窗
    [self closeCollectionView];
    // 记录选中的item
    _index_path = indexPath;
    // 记录地区名字
    self.locationName = _location.location_info.location[indexPath.row].location_name;
    // 更改地区按钮的标题
    [_locationButton setTitle:_location.location_info.location[indexPath.row].location_name forState:UIControlStateNormal];
    _locationButton.selected = !_locationButton.selected;
    // 取出cell
    DropViewCollectionViewCell *cell = (DropViewCollectionViewCell *)[collectionView cellForItemAtIndexPath:_index_path];
    // 改变cell的颜色
    cell.locationLabel.textColor = kDefaultColor;
    // 请求数据
    NSString *djLocationURL = [NSString stringWithFormat:@"http://api.kufm.cn/v3/get_dj/location/%ld/order/1/page_info?bar_height=81&page_index=0&page_size=20&total_height=627", _location.location_info.location[indexPath.row].location_key];
    [GXNetTool GET:djLocationURL andBody:nil andHeader:nil andResponse:GXJSON andSuccessBlock:^(id result) {
        _content = [DJRootModel modelWithDictionary:result];
        // 刷新数据
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"主播页更换地区失败");
    }];
}
#pragma mark - 取消选中item
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    DropViewCollectionViewCell *cell = (DropViewCollectionViewCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]];
    cell.locationLabel.textColor = [UIColor lightGrayColor];
}
#pragma mark - init
- (void)awakeFromNib {
    [super awakeFromNib];
    // tableView
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 注册轮播图cell
    [_tableView registerNib:[UINib nibWithNibName:@"DJTableViewCell" bundle:nil] forCellReuseIdentifier:kDJTableViewCell];
    // 注册正常cell
    [_tableView registerNib:[UINib nibWithNibName:@"DJBannerTableViewCell" bundle:nil] forCellReuseIdentifier:kDJBannerTableViewCell];
    // 地区菜单
    [self setUpZLDropDownMenu];
//    [self createDownPullMenu];
}

#pragma mark - ZLDropDownMenu
- (void)setUpZLDropDownMenu {
    NSArray *mainArray = @[@"BOOKS", @"MOVIES", @"HOME", @"MUSIC"];
    NSArray *subArray = @[@[@"All", @"Arts", @"Photography", @"Biographies", @"Memoirs", @"Business", @"Calendars",
                            @"Education", @"Comics", @"Computers", @"Transpotation", @"History"],
                          @[@"All", @"Animation", @"Drama", @"Comedy"],
                          @[@"All", @"Furniture", @"Bedding", @"Bath", @"Artwork"],
                          @[@"All", @"Blues", @"Christian", @"Classical", @"Country", @"Jazz", @"Pop", @"Folk", @"Gospel"]
                          ];
    _mainTitles = [mainArray mutableCopy];
    _subTitles = [subArray mutableCopy];
    ZLDropDownMenu *menu = [[ZLDropDownMenu alloc] init];
    menu.frame = CGRectMake(0, 0, SCREEN_WIDTH, 30);
    menu.delegate = self;
    menu.dataSource = self;
    [self.contentView addSubview:menu];
}

#pragma mark - model setter
- (void)setContent:(DJRootModel *)content {
    _content = content;
    [self.tableView reloadData];
}
- (void)setLocation:(DJLocationRoot *)location {
    _location = location;
    [self.collectionView reloadData];
}
#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_content) {
        return _content.dj_info.count;
    } else {
        return 20;
    }
}
#pragma mark - tableView 代理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        DJBannerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDJBannerTableViewCell];
        if (_content) {
            cell.banners = _content.dj_info[indexPath.row].banners;
            cell.cycleView.indexPath = ^(NSIndexPath *index) {
                switch (cell.banners[index.row - 1].banner_type) {
                    case 0: {
                       NSLog(@"type 0, 直接跳转播放页");
                    } break;
                    case 1: {
                        NSLog(@"type 1, 跳转节目详情页");
                        DJDetailBannerViewController *playerVC = [[DJDetailBannerViewController alloc] init];
//                        playerVC.playKey = cell.banners[index.row - 1].banner_channel_key;
                        
                        
                        [self.vc.navigationController pushViewController:playerVC animated:YES];
                    } break;
                    case 2: NSLog(@"type 2"); break;
                    case 3: {
                        NSLog(@"type 3, 跳转djDetailVC");
                        DJDetailViewController *djDetailVC = [[DJDetailViewController alloc] init];
                        [self.vc.navigationController pushViewController:djDetailVC animated:YES];
                    } break;
                        
                    default:break;
                }
            };
        }
        return cell;
    } else {
        DJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDJTableViewCell];
        if (_content) {
            cell.info = _content.dj_info[indexPath.row];
        }
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_content.dj_info[indexPath.row].banners) {
        return 120;
    } else if (indexPath.row == 0) {
        return 120;
    } else {
        return 80;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DJDetailViewController *detailVC = [[DJDetailViewController alloc] init];
    detailVC.dj_info = _content.dj_info[indexPath.row];
    id responder = self.nextResponder;
    while (![responder isKindOfClass:[UIViewController class]] && responder != nil) {
        responder = [responder nextResponder];
    }
    UIViewController *vc = (UIViewController *)responder;
    [vc.navigationController pushViewController:detailVC animated:YES];
}
- (UIViewController *)vc {
    id responder = self.nextResponder;
    while (![responder isKindOfClass:[UIViewController class]] && responder != nil) {
        responder = [responder nextResponder];
    }
    return (UIViewController *)responder;
}

#pragma mark - ZLDropDownMenuDataSource

// 分类个数
- (NSInteger)numberOfColumnsInMenu:(ZLDropDownMenu *)menu
{
    return _mainTitles.count;
}

// 每个分类有多少行
- (NSInteger)menu:(ZLDropDownMenu *)menu numberOfRowsInColumns:(NSInteger)column
{
    return [self.subTitles[column] count];
}

// 分类标题
- (NSString *)menu:(ZLDropDownMenu *)menu titleForColumn:(NSInteger)column
{
    return self.mainTitles[column];
}

// 每个分类下的标题
- (NSString *)menu:(ZLDropDownMenu *)menu titleForRowAtIndexPath:(ZLIndexPath *)indexPath
{
    NSArray *array = self.subTitles[indexPath.column];
    return array[indexPath.row];
}
#pragma mark - ZLDropDownMenuDelegate
- (void)menu:(ZLDropDownMenu *)menu didSelectRowAtIndexPath:(ZLIndexPath *)indexPath
{
    NSArray *array = self.subTitles[indexPath.column];
    NSLog(@"%@", array[indexPath.row]);
}

@end
